﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using mock_ehr_lib.Model;
using mock_ehr_lib.Model.Summary;
using mock_ehr_lib.Model.Infobutton;

namespace mock_ehr_lib.View.MainTabs
{
    public partial class Immunization : UserControl
    {
        public Immunization()
        {
            InitializeComponent();
            if (DesignMode) return;
            Load += immunizationLoad;
            MockEHRApplication.Instance.OnPatientChanged += onPatientChanged;
            dgvImmunizations.CellClick += immunizationsCellClick;
            dgvImmunizations.CellMouseEnter += immunizationsCellEnter;
        }

        void immunizationLoad(object sender, EventArgs e)
        {
            var currentPatient = MockEHRApplication.Instance.CurrentPatient;
            if (currentPatient == null) return;
            dgvImmunizations.DataSource = currentPatient.Summary.Immunizations;
            if (dgvImmunizations.Columns["infobutton"] == null)
            {
                dgvImmunizations.Columns.Add(new DataGridViewImageColumn { HeaderText = "", Name = "infobutton" });
            }
            for(var rowIndex = 0; rowIndex < dgvImmunizations.Rows.Count; rowIndex++)
            {
                var immunizationItem = dgvImmunizations.Rows[rowIndex].DataBoundItem as IInfobuttonProvider;
                if (immunizationItem == null) continue;
                var infobuttonParameters = immunizationItem.GetParameters();
                if (infobuttonParameters.Count() == 0) continue;
                var cell = dgvImmunizations["infobutton", rowIndex] as DataGridViewImageCell;
                if (cell == null) continue;
                cell.ValueType = Properties.Resources.infobutton.GetType();
                cell.Value = Properties.Resources.infobutton;
            }
            dgvImmunizations.Refresh();
        }

        void immunizationsCellEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex == -1 || e.ColumnIndex == -1) return;
             if(dgvImmunizations.Columns[e.ColumnIndex].Name == "infobutton")
             {
                 var infobuttonProvider = dgvImmunizations.Rows[e.RowIndex].DataBoundItem as IInfobuttonProvider;
                 if (infobuttonProvider == null) return;
                 if(infobuttonProvider.GetParameters().Length > 0)
                 {
                     dgvImmunizations.Cursor = Cursors.Hand;
                     return;
                 }
             }
            dgvImmunizations.Cursor = Cursors.Default;
        }

        void immunizationsCellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex == -1 || e.ColumnIndex == -1) return;
            if (dgvImmunizations.Columns[e.ColumnIndex].Name != "infobutton") return;
            var infobuttonProvider = dgvImmunizations.Rows[e.RowIndex].DataBoundItem as IInfobuttonProvider;
            if (infobuttonProvider == null) return;
            var paramString = "";
            foreach(var parameter in infobuttonProvider.GetParameters())
            {
                paramString += "&" + parameter.Name + "=" + parameter.Value;
            }
            dgvImmunizations[e.ColumnIndex, e.RowIndex].Selected = false;
            System.Diagnostics.Process.Start(MockEHRApplication.Instance.InfoButtonBaseUrl + paramString);
        }

        void onPatientChanged(Patient newPatient)
        {
            immunizationLoad(this, null);
        }
    }
}
